package com.xyz.apis.repository;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xyz.admin.common.entity.EvUserEntity;
import com.xyz.admin.common.global.EvTicketRedisKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoRepository {

    @Autowired
    JedisService expandJedisService;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private Map<String, EvUserEntity> userInfoMap = new HashMap<>();


    /**
     * 缓存用户信息
     */
    public void setUserInfo(EvUserEntity evUserEntity){
        try {
            evUserEntity.setPassword("***");
            expandJedisService.hset(EvTicketRedisKey.evapi_userinfo_key, evUserEntity.getUserid().toString(), JSONObject.toJSONString(evUserEntity));
            userInfoMap.put(evUserEntity.getUserid().toString(), evUserEntity);
        } catch (Exception e) {
            logger.error("缓存用户信息异常",e);
        }
    }


    /**
     * 获取用户信息
     */
    public EvUserEntity getUserInfo(Long userId){
        String userid = userId.toString();
        try {
            EvUserEntity evUserEntity = userInfoMap.get(userid);
            if(null==evUserEntity){
                evUserEntity = JSON.parseObject(expandJedisService.hget(EvTicketRedisKey.evapi_userinfo_key, userid), EvUserEntity.class);
                userInfoMap.put(userid, evUserEntity);
            }
            return evUserEntity;
        } catch (Exception e) {
            logger.error("redis获取用户信息异常",e);
        }
        return null;

    }


    /**
     * 获取多个用户信息
     */
    public Map<String, EvUserEntity> getUsersInfo(List<Long> userIds){
        try {
            Map<String, EvUserEntity> userEntityMap = new HashMap<>();
            List<String> userIdsNofound = new ArrayList<>();
            for(Long userId : userIds){
                if(userId == null){
                    continue;
                }
                String userid = userId.toString();
                EvUserEntity evUserEntity = userInfoMap.get(userid);
                if(null==evUserEntity){
                    userIdsNofound.add(userid);
                }
                userEntityMap.put(userid, evUserEntity);
            }
            if(userIdsNofound.size() != 0){
                List<String> userInfoStr = expandJedisService.hmget(EvTicketRedisKey.evapi_userinfo_key, userIdsNofound.toArray(new String[userIdsNofound.size()]));
                for(String userInfo : userInfoStr){
                    if(null == userInfo || "".equals(userInfo)){
                        continue;
                    }
                    EvUserEntity evUserEntity = JSON.parseObject(userInfo, EvUserEntity.class);
                    userInfoMap.put(evUserEntity.getUserid().toString(), evUserEntity);
                }
            }
            return userEntityMap;
        } catch (Exception e) {
            logger.error("redis获取多个用户信息异常",e);
        }
        return null;

    }

    /**
     * 获取多个用户信息
     */
    public List<EvUserEntity> getUsersList(List<String> userIds){
        try {
            Map<String, EvUserEntity> userEntityMap = new HashMap<>();
            List<EvUserEntity> listUser = new ArrayList<>();
            List<String> userIdsNofound = new ArrayList<>();
            userIds.stream().forEach(p->{
                if(null==p){
                    return;
                }
                EvUserEntity evUserEntity = userInfoMap.get(p);
                if(null==evUserEntity){
                    userIdsNofound.add(p);
                }else {
                    listUser.add(evUserEntity);
                }
            });

            if(userIdsNofound.size() != 0){
                List<String> userInfoStr = expandJedisService.hmget(EvTicketRedisKey.evapi_userinfo_key, userIdsNofound.stream().toArray(String[]::new));
                /*for(String userInfo : userInfoStr){
                    if(null == userInfo || "".equals(userInfo)){
                        continue;
                    }
                    EvUserEntity evUserEntity = JSON.parseObject(userInfo, EvUserEntity.class);
                    userInfoMap.put(evUserEntity.getUserid().toString(), evUserEntity);
                }*/
                userInfoStr.stream().forEach(p->{
                    if(null == p || "".equals(p)){
                        return;
                    }
                    listUser.add(JSON.parseObject(p, EvUserEntity.class));
                });
            }
            return listUser;

        } catch (Exception e) {
            logger.error("redis获取多个用户信息异常",e);
        }
        return null;

    }









}
